package com.hangzhou.welbeing.welbeinginstrument.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.activity.ReleaseEditingActivity;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemClickListener;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;

import java.util.ArrayList;

/**
 * 杭州惟君医疗科技有限公司11
 * Created by 20433 on 2018/2/3
 */

public class MyEditingPictureAdapter extends  RecyclerView.Adapter<MyEditingPictureAdapter.ViewHolder> implements View.OnClickListener{
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private ArrayList<ImageItem> imagesdate = new ArrayList<>();
    private ReleaseEditingActivity mcontext;
    private ImagePicker imagePickerdate = null;
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    public MyEditingPictureAdapter(ArrayList<ImageItem> images, ReleaseEditingActivity mContext, ImagePicker imagePicker) {
        imagesdate.addAll(images);
        mcontext = mContext;
        imagePickerdate = imagePicker;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_editing_picture_recyclerview,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;
    }
    public void onBindViewHolder(ViewHolder holder, final int position) {

        // todo  真正显示图片的代码
        imagePickerdate.getImageLoader().displayImage( mcontext, imagesdate.get(position).path, holder.iv_picture, 160, 150);
        holder.rl_picture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnItemClickListener != null ){
                    mOnItemClickListener.onItemFirstButtonClick(v,position);
                }
            }
        });
    }
    public  void delectItem(int position){
        imagesdate.remove(position);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return imagesdate.size();

    }

    @Override
    public void onClick(View v) {
        if(mOnItemClickListener != null ){
            mOnItemClickListener.onItemClick(v);
        }
    }


    /**
     * 身体布局Holder
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_picture;
        private RelativeLayout rl_picture;
        public ViewHolder(View itemView) {
            super(itemView);
            iv_picture =  (ImageView) itemView.findViewById(R.id.item_editing_picture_recyclerview_iv_picture);
            rl_picture =  (RelativeLayout) itemView.findViewById(R.id.item_editing_picture_recyclerview_rl_picture);
        }
    }
}
